/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package eypweb_bsc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javafx.collections.ObservableList;
import javafx.stage.FileChooser;
import javafx.stage.Stage;


/**
 *
 * @author Kohalmi Istvan
 */

// CSV fajlkezelo osztaly
public class CSVHandler{
    
    // jelenleg az osztaly allapotmentes, 
    // kesobb szukseg lehet peldanyositasra
    public CSVHandler(){}
    
    // deviza torzs exportalasa
    public static File exportCurrency(ObservableList<Currency> list){
        // ablak referencia elkerese
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        // uj mento kepernyo
        FileChooser fc = new FileChooser();
        // csak a CSV fajlokat mutassa
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        // kepernyo mutatasa
        File exportCSV = fc.showSaveDialog(stage);
        try{
            // adatfolyam iro letrehozasa
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            // fejlec kiirasa fajlba
            out.write("Megnevezés,Egység,Hatály kezdete,Hatály vége\n");
            // elemek listazasa a fajlba
            for (Currency cur : list)
            {
                out.append(cur.getName()+","+cur.getUnit()+","+cur.getStartDate()+","+cur.getEndDate()+"\n");
            }
            // adatfolyam lezarasa
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        // fajl referenciajaval ter vissza, 
        // ha esetleg szukseg lenne ra
        return exportCSV;
    }
    
    // eszkozcsoport exportalasa
    public static File exportInstrumentGroup(ObservableList<InstrumentGroup> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportCSV = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            out.write("Megnevezés,Hatály kezdete,Hatály vége\n");
            for (InstrumentGroup iGrp : list)
            {
                out.append(iGrp.getName()+","+iGrp.getStartDate()+","+iGrp.getEndDate()+"\n");
            }
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportCSV;
    }
    
    // befektetes csoport exportalasa
    public static File exportInvestmentGroup(ObservableList<InvestmentGroup> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportCSV = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            out.write("Megnevezés,Megjegyzés,Hatály kezdete,Hatály vége\n");
            for (InvestmentGroup iGrp : list)
            {
                out.append(iGrp.getName()+","+iGrp.getNote()+","+iGrp.getStartDate()+","+iGrp.getEndDate()+"\n");
            }
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportCSV;
    }
    
    // portfoliok exportalasa
    public static File exportPortfolio(ObservableList<Portfolio> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportCSV = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            out.write("Megnevezés,Hatály kezdete,Hatály vége\n");
            for (Portfolio port : list)
            {
                out.append(port.getName()+","+port.getStartDate()+","+port.getEndDate()+"\n");
            }
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportCSV;
    }
    
    // ertekpapriok exportalasa
    public static File exportSecurities(ObservableList<Securities> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportCSV = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            out.write("Azonosító,Megnevezés,Eszközcsoport,Befektetési csoport,Devizanem,"
                    + "Alapcímlet,71OJ,71OJ_C,71OK,Lejárat,"
                    + "Hatály kezdete,Hatály vége,Elszámolás alapja\n");
            for (Securities secr : list)
            {
                out.append(secr.getISIN()+","+secr.getName()+","+secr.getInstrument()+","
                        +secr.getInvestment()+","+secr.getCurrency()+","+secr.getDenomination()
                        +","+secr.getTIOJ()+","+secr.getTIOJ_C()+","+ secr.getTIOK()+","
                        +secr.getExpiry()+","+secr.getStartDate()+","+secr.getEndDate()
                        +secr.getAccountBase()+"\n");
            }
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportCSV;
    }
    
    // tranzakciotipusok exportalasa
    public static File exportTransaction(ObservableList<Transaction> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportCSV = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            out.write("Megnevezés,Darabszám változás,Beszerzési érték csökkentés,Elhatárolt kamat,Elhatárolt osztalék,Piaci árfolyam nyereség,Deviza árfolyam nyereség,Hatály kezdete,Hatály vége\n");
            for (Transaction tran : list)
            {
                out.append(tran.getName()+","+tran.getDeltaVolume()+","+tran.getDecrAV()+","
                        +tran.getSepInterest()+","+tran.getSepDividend()+","+tran.getMktRateGain()
                        +","+tran.getCurRateGain()+","+tran.getStartDate()+","+tran.getEndDate()+"\n");
            }
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportCSV;
    }
    
    // vagyonkezeolok exportalasa
    public static File exportTrustee(ObservableList<Trustee> list){
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportCSV = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            out.write("Név,Rövid név,Irányítószám,Település,Cím,"
                    + "Telefon,Mobil,Fax,E-mail,Kapcsolat,"
                    + "Beosztás,Hatály kezdete,Hatály vége\n");
            for (Trustee trst : list)
            {
                out.append(trst.getName()+","+trst.getShortName()+","+trst.getZip()+","
                        +trst.getCity()+","+trst.getAddress()+","+trst.getTel()+","
                        +trst.getMobile()+","+trst.getFax()+","+trst.getMail()+","
                        +trst.getConnection()+","+trst.getAssignment()+","+trst.getStartDate()
                        +","+trst.getStartDate()+trst.getEndDate()+"\n");
            }
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportCSV;
    }
    
    // muveletek exportalasa
    public static File exportOperation(ObservableList<Inventory> list)
    {
        Director director = Director.getInstance();
        Stage stage = director.getStage();
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Comma-separated values (*.csv)", "*.csv");
        fc.getExtensionFilters().add(filter);
        fc.setTitle("CSV exportálása...");
        fc.setInitialDirectory(File.listRoots()[0]);
        File exportCSV = fc.showSaveDialog(stage);
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportCSV),"utf-8"));
            out.write("Vételi ID,Beszerzés dátuma,Mennyiség,"
                        + "Névérték,Beszerzési érték,Piaci érték,"
                        + "ÉK - kamat,ÉK - osztalék,ÉK - egyéb piaci,"
                        + "ÉK - deviza árfolyam-különbözet,Ügyletszám,"
                        + "Elszámolt mennyiség/névérték,Könyv szerinti érték,"
                        + "Vételárban elszámolt kamat,Realizált kamat,"
                        + "Realizált osztalék,Realizált egyéb piaci,"
                        + "Realizált deviza árfolyam-különbözet,Kivezetett kamat,"
                        + "Kivezetett osztalék,Kivezetett egyéb piaci,"
                        + "Kivezetett deviza árfolyam-különbözet\n");
            for (Inventory inv : list)
            {
                out.append(inv.getPurchaseId()+","+inv.getAcquisitionDate()+","+inv.getQuantity()+","
                        + inv.getNominal()+","+inv.getAcquisitionValue()+","+inv.getMarketValue()+","
                        + inv.getDerecogniseInterest()+","+inv.getDerecogniseDividend()+","+inv.getDerecogniseRateDelta()+","
                        + inv.getDerecogniseCurrencyRateDelta()+","+inv.getSerial()+","
                        + inv.getAccountedBase()+","+inv.getAccountedValue()+","
                        + inv.getAccumulatedInterestAccountedPrice()+","+inv.getRealisedInterest()+","
                        + inv.getRealisedDividend()+","+inv.getRealisedRateDelta()+","
                        + inv.getRealisedCurrencyRateDelta()+","+inv.getDerecognisedInterest()+","
                        + inv.getDerecogniseDividend()+","+inv.getDerecognisedRateDelta()+","
                        + inv.getDerecognisedCurrencyRateDelta()+"\n");
            }
            out.close();
        }
        catch(Exception e)
        {
            System.err.println("Írási hiba \n" + e.getMessage());
        }
        return exportCSV;
    }
}
